<?php

namespace app\admin\controller\music\config;

use app\BaseController;
use app\model\music\MusicModel;
use app\model\music\MusicTypeModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 类型列表
     * @return bool|string
     */
    public function TypeList(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['name'])) {
                $where[] = ['name', 'like', '%' . $param['name'] . '%'];
            }
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $MusicTypeModel = new MusicTypeModel();
            $data = $MusicTypeModel->where($where)->order('sort','asc')->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 类型添加
     * @return bool|string
     */
    public function TypeAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'name|名称' => 'require',
            ]);
            $MusicTypeModel = new MusicTypeModel();
            $param['unit_id'] = $this->userData['unit_id'];
            $param['time'] = time();
            $MusicTypeModel->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 类型修改
     * @return bool|string
     */
    public function TypeEdit(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require|integer|max:11',
                'name|名称' => 'require|string',
            ]);
            $MusicTypeModel = new MusicTypeModel();
            $info = $MusicTypeModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 类型删除
     * @return bool|string
     */
    public function TypeDel(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require|integer|max:11',
            ]);
            $MusicTypeModel = new MusicTypeModel();
            $info = $MusicTypeModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->is_del = 1;
            $info->del_time = time();
            $info->save();
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 音乐列表
     * @return bool|string
     */
    public function List(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页数量' => 'require|integer',
            ]);
            $where = [];
            if (!empty($param['name'])) {
                $where[] = ['name', 'like', '%' . $param['name'] . '%'];
            }
            if (!empty($param['type_id'])) {
                $where[] = ['type_id', '=', $param['type_id']];
            }
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $MusicModel = new MusicModel();
            $query = $MusicModel->where($where);
            $data = $query->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$value) {
                $value['label'] = explode(',', $value['label']);
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }


    /**
     * 音乐详情
     * @return bool|string
     */
    public function info(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $MusicModel = new MusicModel();
            $info = $MusicModel->find($id);
            if (!$info) throw new \Exception('参数错误！');
            $data = $info->toArray();
            if (!empty($data['label'])){
                $data['label'] = explode(',', $data['label']);
            }else{
                $data['label'] = [];
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }


    /**
     * 音乐批量添加
     * @return bool|string
     */
    public function batchAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'type_id|类型' => 'require|integer|max:11',
                'file|文件地址' => 'require',
            ]);
            $data = [];
            if (!empty($param['label'])) {
                $label = implode(',', $param['label']);
            }
            foreach ($param['file'] as $file) {
                $data[] = [
                    'unit_id' => $this->userData['unit_id'],
                    'type_id' => $param['type_id'],
                    'name' => $file['name'],
                    'img' => env('SETTING.RATING_MUSIC'),
                    'file' => $file['file'],
                    'label' => $label??'',
                    'duration' => $file['duration']??0,
                    'time' => time()
                ];
            }
            if (!empty($data)) {
                $MusicModel = new MusicModel();
                $MusicModel->insertAll($data);
            }
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 音乐添加
     * @return bool|string
     */
    public function Add(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'type_id|类型' => 'require|integer|max:11',
                'file|文件地址' => 'require|string',
                'img|图片地址' => 'require|string',
            ]);
            $param['unit_id'] = $this->userData['unit_id'];
            $param['time'] = time();
            if (!empty($param['label'])) {
                $param['label'] = implode(',', $param['label']);
            }
            $MusicModel = new MusicModel();
            $MusicModel->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 音乐编辑
     * @return bool|string
     */
    public function Edit(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require|integer|max:11',
                'type_id|类型' => 'require|integer|max:11',
                'file|文件地址' => 'require|string',
                'img|图片地址' => 'require|string',
            ]);
            $MusicModel = new MusicModel();
            $info = $MusicModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (!empty($param['label'])) {
                $param['label'] = implode(',', $param['label']);
            }
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 音乐删除
     * @return bool|string
     */
    public function Del(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require|integer|max:11',
            ]);
            $MusicModel = new MusicModel();
            $info = $MusicModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->is_del = 1;
            $info->del_time = time();
            $info->save();
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 音乐批量删除
     * @return bool|string
     */
    public function DelAll(): bool|string
    {
        try {
            $id = $this->request->param('id');
            if (!is_array($id)) throw new \Exception('参数错误！');
            $MusicModel = new MusicModel();
            $MusicModel->where('id', 'in', $id)->update([
                'is_del' => 1,
                'del_time' => time()
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 音乐修改类型
     * @return bool|string
     */
    public function EditType(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require',
                'type_id|分类' => 'require',
            ]);
            if (!is_array($param['id'])) throw new \Exception('参数错误！');
            $MusicModel = new MusicModel();
            $MusicModel->where('id','in', $param['id'])->update([
                'type_id' => $param['type_id'],
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }


}